﻿using System;
using System.Diagnostics;

namespace MikeKappel.Com.CS
{
    public static partial class SortingAlgorithms
    {
        public delegate Boolean BubbleDelegate(Int32 currentIndex, Int32 currentValue);
        
        public static void BubbleSort(BubbleDelegate onSort, Int32[] items)
        {
            for (int pass = 1; pass < items.Length; pass++)
                for (int i = 0; i < items.Length - 1; i++)
                    if (items[i] > items[i + 1])
                    {
                        Int32 hold = items[i];
                        items[i] = items[i + 1];
                        items[ i + 1 ] = hold;
                        if (onSort != null && !onSort(i, items[i]))
                                goto escape;
                    }
        escape:
            return;
        }
        public static void BubbleSort(Int32[] items)
        {
            BubbleSort(null, items);
        }
    }
}
